<!--
 * @Description: file content
-->
<template>
  <div>
    <el-button :icon="Back" @click="goBack" />
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title" autocomplete="off" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <Editor @change="changeEditor" :contentHtml="ruleForm.content" />
      </el-form-item>
      <el-form-item label="类别" prop="category">
        <el-select v-model="ruleForm.category">
          <el-option
            v-for="item in NEWS_CATEGORY"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="封面图片" prop="coverPicture">
        <Upload :avatar="ruleForm.coverPicture" @change="handleChange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import { ref, reactive, onMounted,  } from 'vue';
import { ElMessage } from 'element-plus';
import uploadFun from '@/util/uploadFun';
import Upload from '@/components/upload/Upload.vue';
import Editor from '@/components/editor/Editor.vue';
import { useRouter, useRoute } from 'vue-router';
import { Back } from '@element-plus/icons-vue';
import { NEWS_CATEGORY,  } from '@public/index.js';
import axios from 'axios';

const router = useRouter();
const route = useRoute();
const id = route.params.id;
const ruleFormRef = ref();
// 内容 HTML
const valueHtml = ref('');
const ruleForm = reactive({
  category: 1,
  title: '',
  content: '',
  coverPicture: '',
  file: null,
  isPublish: 0 //0未发布，1发布
});
const rules = reactive({
  title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  content: [{ required: true, message: '请输入内容', trigger: 'blur' }],
  category: [{ required: true, message: '请选择类别', trigger: 'blur' }],
  coverPicture: [{ required: true, message: '请选择封面图片', trigger: 'change' }]
});
const handleChange = (file) => {
  ruleForm.coverPicture = URL.createObjectURL(file);
  ruleForm.file = file;
};
const changeEditor = (val) => {
  ruleForm.content = val.getHtml();
};
const goBack = () => {
  router.back();
};
const getData = async () => {
  axios.get(`/adminApi/news/list/${id}`).then((res) => {
    if (res.code === 200) {
      Object.assign(ruleForm,res.data[0]);
      valueHtml.value=res.data[0].content;
    }
  });
};
const submitForm = () => {
  ruleFormRef.value.validate(async (valid) => {
    if (valid) {
      const res = await uploadFun('/adminApi/news/list', ruleForm);
      if (res.code === 200) {
        ElMessage({
          message: res.msg,
          type: 'success'
        });
        router.push('/news-manage/news-list');
      }
    }
  });
};
onMounted(() => {
  getData();
});
</script>
<style lang="scss"></style>
